#include "web.h"
#include <QLineEdit>
#include <QPushButton>
#include <QtWebKit/QWebView>
#include <QGridLayout>
#include <QFrame>
#include <QWSServer>


#include <QDebug>
#include <QString>
#include <QSslSocket>
#include <openssl/ssl.h>
#include <openssl/crypto.h>

#include <QSsl>
#include <QSslConfiguration>

#define INITWEBADRESS "https://www.hao123.com"

Web::Web(QWidget *parent) : JDialog(parent)
{

    m_manager = new QNetworkAccessManager(this);
//    m_request = new QNetworkRequest(QUrl( INITWEBADRESS));
    m_request = new QNetworkRequest();

    QSslConfiguration conf = m_request->sslConfiguration();
    conf.setPeerVerifyMode(QSslSocket::VerifyNone);
    conf.setProtocol(QSsl::TlsV1SslV3);
    m_request->setSslConfiguration(conf);

    m_request->setUrl(QUrl(INITWEBADRESS));
    m_reply = m_manager->get(*m_request);
    m_reply->ignoreSslErrors();


    im = new InputMethod;
//    DebugOrRelease=0;
    m_pUrlEdit = new QLineEdit();
    m_pUrlEdit->setText(INITWEBADRESS);
    m_pUrlEdit->installEventFilter(im);
    m_pUrlEdit->setObjectName("m_pUrlEdit");
    connect(im,SIGNAL(setImObjName(QString)),this,SLOT(createImConnect(QString)));//connect the input method

    m_pWebView = new QWebView();
//    m_pWebView->load(QUrl(INITWEBADRESS));
    m_pWebView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);


    QPushButton *pGoBtn = new QPushButton(tr("Go"));
    QPushButton *pBackBtn = new QPushButton(tr("<"));
    QGridLayout *pSubLayout = new QGridLayout();

    pSubLayout->addWidget(m_pUrlEdit, 0, 0, 1, 10);
    pSubLayout->addWidget(pBackBtn, 0, 10, 1, 1);
    pSubLayout->addWidget(pGoBtn, 0, 11, 1, 2);
    pSubLayout->addWidget(m_pWebView, 1, 0, 5, 13);
    pSubLayout->setSpacing(10);

    QFrame *pFrame = new QFrame();
    pFrame->setLayout(pSubLayout);
    pFrame->setObjectName("webFrame");

    QGridLayout *pMainLayout = new QGridLayout();
    pMainLayout->addWidget(pFrame);

    connect(pGoBtn, SIGNAL(clicked(bool)), this, SLOT(slotGoBtnClicked()));
    connect(m_pWebView, SIGNAL(loadFinished(bool)), this, SLOT(slotCheckLoad(bool)));
 //   connect(m_pWebView, SIGNAL(urlChanged(QUrl)), this, SLOT(slotUrlChanged(QUrl)));
    connect(m_pWebView, SIGNAL(linkClicked(QUrl)), this, SLOT(slotLinkClicked(QUrl)));
    connect(pBackBtn, SIGNAL(clicked(bool)), m_pWebView, SLOT(back()));


//    connect(m_reply , SIGNAL(readyRead()), this , SLOT(slotreadyRead()));
//    connect(m_reply , SIGNAL(finished()) , this , SLOT(slotFinished( )));
    if (DebugOrRelease)
    this->setCursor(Qt::CrossCursor);
    this->setJLayout(pMainLayout);
    this->setJTitle(tr("浏览器"));

    QTimer*timerRead = new QTimer(this);
    timerRead->start(1000);
    connect(timerRead,SIGNAL(timeout()),this,SLOT(slotreadyRead()));

}

void Web::createImConnect(QString ObjName)
{
    disconnect(im->keyboard,SIGNAL(setvalue(QString)),m_pUrlEdit,SLOT(setText(QString)));

    if(ObjName=="m_pUrlEdit")
        connect(im->keyboard,SIGNAL(setvalue(QString)),m_pUrlEdit,SLOT(setText(QString)));
}

void Web::slotGoBtnClicked()
{
   // m_pWebView->load(QUrl(m_pUrlEdit->text()));
    qDebug() << "slotGoBtnClicked";
    qDebug() << m_pUrlEdit->text();
    m_request->setUrl(QUrl(m_pUrlEdit->text()));
    m_reply = m_manager->get(*m_request);

}

void Web::slotLinkClicked(QUrl url)
{
    qDebug() << "slotLinkClicked";
    qDebug() << url;
//    m_pWebView->load(url);

    m_request->setUrl(QUrl(url));
    m_reply = m_manager->get(*m_request);
}

void Web::slotCheckLoad(bool isLoad)
{
    if(isLoad) {
        //"load success");
    }
    else {
        //"load fail");
    }
}

void Web::slotUrlChanged(QUrl url)
{
    m_pUrlEdit->setText(url.toString());
}


void Web::slotreadyRead( )
{
    qDebug() << "slotreadyRead";

    //转换为utf-8编码，这样就可以显示中文了
    QTextCodec *codec = QTextCodec::codecForName("utf8");
    QString getData = codec->toUnicode(m_reply->readAll());

    qDebug() << getData.toUtf8();
    if( !getData.isEmpty())
        m_pWebView->setHtml(getData.toUtf8());
  //  m_reply->setFinished(true);

}

void Web::slotFinished()
{
    qDebug() << "slotOnFinished";
    //转换为utf-8编码，这样就可以显示中文了
    QTextCodec *codec = QTextCodec::codecForName("utf8");
    QString getData = codec->toUnicode(m_reply->readAll());

    qDebug() << getData.toUtf8();


    //m_pWebView->setHtml(getData.toUtf8());
   // m_reply->close();
  //  m_reply->setFinished(true);
  /*
    //输出文件
    QFile out_file("One.html");
    out_file.open(QIODevice::Text | QIODevice::WriteOnly);
    out_file.write(getData.toUtf8());
    out_file.close();
    */
}


Web::~Web()
{
	if(!DebugOrRelease)
	{
		QApplication::setOverrideCursor(Qt::BlankCursor); 
		QWSServer::setCursorVisible(false);
	}

}





